package ecnu.ecgbackend.controller;
import ecnu.ecgbackend.entity.User;
import ecnu.ecgbackend.service.DiagnosisUserService;
import ecnu.ecgbackend.utils.Result;
import ecnu.ecgbackend.vo.DeveloperVo;
import ecnu.ecgbackend.vo.DoctorVo;
import ecnu.ecgbackend.vo.PatientVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;

@RestController
@RequestMapping("/user")
public class DiagnosisUserController {
    @Resource
    DiagnosisUserService diagnosisUserService;
    @PreAuthorize("hasAuthority('patient')")
    @PostMapping("/patient/update")
    public Result modifyPatientInfo(@RequestBody PatientVo patientVo){
        return diagnosisUserService.editPatientInfo(patientVo.getUser(),patientVo.getPatient());
    }
    @PreAuthorize("hasAuthority('doctor')")
    @PostMapping("/doctor/update")
    public Result modifyDoctorInfo(@RequestBody DoctorVo doctorVo){
        return diagnosisUserService.editDoctorInfo(doctorVo.getUser(),doctorVo.getDoctor());
    }
    @PreAuthorize("hasAuthority('developer')")
    @PostMapping("/developer/update")
    public Result modifyDeveloperInfo(@RequestBody DeveloperVo developerVo){
        return diagnosisUserService.editDeveloperInfo(developerVo.getUser(),developerVo.getDeveloper());
    }
    @PreAuthorize("hasAnyAuthority('admin')")
    @PostMapping("/admin/update")
    public Result modifyAdminInfo(@RequestBody User user){
        return diagnosisUserService.editAminInfo(user);
    }
    @PostMapping("/phone")
    public Result modifyPhone(String phone,String code){
        return diagnosisUserService.editPhone(phone,code);
    }
    @GetMapping("/info")
    public Result queryUserInfo(){
        return Result.ok(diagnosisUserService.getUserInfo());
    }
    @PreAuthorize("hasAuthority('doctor')")
    @PostMapping("/doctor/certify")
    public Result modifyDoctorCertify(@RequestParam("certifyFile") MultipartFile multipartFile) throws IOException {
        try{
            if(diagnosisUserService.editDoctorCertify(multipartFile)){
                return Result.ok().message("资格证书上传成功！");
            }
            return Result.error().message("资格证书上传失败！");
        }catch (IOException e){
            return Result.error().code(500).message(e.getMessage());
        }
    }
    @PreAuthorize("hasAnyAuthority('patient','developer')")
    @GetMapping("/integral")
    public Result queryIntegral(){
        int integral= diagnosisUserService.getIntegral();
        if(integral>-1){
            return Result.ok(integral).message("获取积分成功！");
        }else {
            return Result.error().message("获取积分失败");
        }
    }
    @PreAuthorize("hasAnyAuthority('doctor','admin')")
    @GetMapping("/certain/info")
    public Result queryCertainUserInfo(@RequestParam("id") int id,
                              @RequestParam("role") String role){
        return Result.ok(diagnosisUserService.getUserInfoByIdAndRole(id,role));
    }
    @PreAuthorize("hasAnyAuthority('patient','developer')")
    @GetMapping("/doctor/list")
    public Result queryDoctorList(){
        return Result.ok(diagnosisUserService.getDoctors());
    }
    @PreAuthorize("hasAnyAuthority('patient','developer')")
    @PostMapping("/artificial")
    public Result addArtificialDiagnosis(Long doctorId,String rawDataId){
        return diagnosisUserService.saveArtificialDiagnosis(doctorId,rawDataId);
    }
    @PreAuthorize("hasAuthority('doctor')")
    @GetMapping("/waitrequest")
    public Result queryWaitRequestPatient(){
        return Result.ok(diagnosisUserService.getWaitRequestPatient());
    }
    @PreAuthorize("hasAuthority('doctor')")
    @GetMapping("/request")
    public Result queryMyRequestPatient(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize){
        return Result.ok(diagnosisUserService.pageMyRequestPatient(pageNum,pageSize));
    }
    @PostMapping("/cancel/request")
    public Result deleteArtificialDiagnosis(int patientId,int doctorId,String rawDataId ){
        if(diagnosisUserService.removeArtificialDiagnosis(patientId,doctorId,rawDataId)){
            return Result.ok().message("取消选择成功！");
        }
        return Result.error().message("取消选择失败");
    }
    @PreAuthorize("hasAuthority('doctor')")
    @PostMapping("/request/accept")
    public Result acceptPatientRequest(int patientId,String rawDataId){
        if(diagnosisUserService.editRequestStatusAccept(patientId,rawDataId)){
            return Result.ok().message("接受诊断成功！");
        }
        return Result.error().message("接受诊断失败");
    }
    @PreAuthorize("hasAuthority('doctor')")
    @PostMapping("/request/reject")
    public Result rejectPatientRequest(int patientId,String rawDataId){
        if(diagnosisUserService.editRequestStatusReject(patientId,rawDataId)){
            return Result.ok().message("拒绝诊断成功！");
        }
        return Result.error().message("接受诊断失败");
    }
    @PreAuthorize("hasAnyAuthority('patient','developer')")
    @GetMapping("/history")
    public Result getHistoryService(@RequestParam("category") String category,
                                    @RequestParam("pageNum") int pageNum,
                                    @RequestParam("pageSize") int pageSize){
        return Result.ok(diagnosisUserService.getHistoryService(category,pageNum,pageSize));
    }

}
